package org.wanda.pv.service.business.impl;

import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.springframework.stereotype.Service;
import org.wanda.pv.framework.base.Result;
import org.wanda.pv.model.business.Mark;
import org.wanda.pv.model.business.MarkExaminesContent;
import org.wanda.pv.model.hy.HyRole;
import org.wanda.pv.model.hy.HyUserInfo;
import org.wanda.pv.service.BaseServiceImpl;
import org.wanda.pv.service.business.MarkExaminesContentService;
import org.wanda.pv.util.HyUtil;

/**
 * @author lxy
 * Created by 2019/1/8 10:48
 */

@Service
public class MarkExaminesContentServiceImpl extends BaseServiceImpl<MarkExaminesContent> implements MarkExaminesContentService {
	public MarkExaminesContentServiceImpl(Dao dao) {
		super(dao);
	}

	@Override
	public Object add(String content, HyUserInfo hyUserInfo, Mark mark) {
		MarkExaminesContent markExaminesContent1 = getMarkExaminesContentByMarkId(mark.getId());
		MarkExaminesContent markExaminesContent2 = null;
		if (markExaminesContent1 == null) {
			markExaminesContent2 = new MarkExaminesContent();
		} else {
			markExaminesContent2 = markExaminesContent1;
		}
		HyRole hyRole = hyUserInfo.getHyRole();
		if (HyUtil.WDROLENAME.equals(hyRole.getRoleType())) {
			markExaminesContent2.setWdExaminesContent(content);
			markExaminesContent2.setWdUserId(hyUserInfo.getId());
		} else if (HyUtil.DEROLENAME.equals(hyRole.getRoleType())) {
			markExaminesContent2.setDeExaminesContent(content);
			markExaminesContent2.setDeUserId(hyUserInfo.getId());
		} else if (HyUtil.PVROLENAME.equals(hyRole.getRoleType())) {
			markExaminesContent2.setPvExaminesContent(content);
			markExaminesContent2.setPvUserId(hyUserInfo.getId());
		} else {
			return Result.error("审查回复不能为空！", "审查回复不能为空！");
		}
		markExaminesContent2.setMarkId(mark.getId());
		if (markExaminesContent1 != null) {
			dao().update(markExaminesContent2);
			markExaminesContent2 = dao().fetch(MarkExaminesContent.class, Cnd.where("id", "=", markExaminesContent2.getId()).and("delFlag", "=", false));
		} else {
			markExaminesContent2 = dao().insert(markExaminesContent2);
			mark.setMarkExaminesContentId(markExaminesContent2.getId());
			dao().update(mark);
		}
		return Result.success("success", markExaminesContent2);
	}

	@Override
	public MarkExaminesContent getMarkExaminesContentByMarkId(Long markId) {
		MarkExaminesContent markExaminesContent = dao().fetch(MarkExaminesContent.class, Cnd.where("mark_id", "=", markId).and("delFlag", "=", false));
		return markExaminesContent;
	}
}
